const daoCommon = require('../util/daoCommon');
const commonUtils = require('../util/commonUtils');

const dao = {
    handleAddAdvert: (data) => {
        let sql;
        const id = commonUtils.genUUID();
        const date = new Date();
        const createTime = date.toLocaleString();
        data.id = id;
        data.createTime = createTime;
        sql = `INSERT INTO advertisement VALUES ($id, $title, $type, $target,$createTime,null,null,null,'0')`;
        return daoCommon.queryCommon(sql, data);
    },
    handleEditAdvert: (data) => {
        let sql;
        const date = new Date();
        const updatedTime = date.toLocaleString();
        data.updatedTime = updatedTime;
        sql = `UPDATE advertisement SET advertisement_title=$title, advertisement_type=$type, advertisement_target=$target,updated_time=$updatedTime where advertisement_id=$id`;
        return daoCommon.queryCommon(sql, data);
    },
    handleDeleteAdvert: (data) => {
        let sql;
        sql = `UPDATE advertisement SET is_delete='1' where advertisement_id=$id`;
        return daoCommon.queryCommon(sql, data);
    },
    handleGetAdvert: () => {
        let sql;
        sql = "select advertisement_id as id,advertisement_title as title,advertisement_type as type,advertisement_target as target,created_time as time,is_delete as state from advertisement where is_delete<>'1' order by created_time";
        return daoCommon.selectCommon(sql);
    },
    handleGetTargetData: (data) => {
        let sql;
        if (data.type == "1") {
            sql = "select case_name as name,case_mainImg as url from cases where case_id=$target";
        } else if (data.type == "2") {
            sql = "select service_name as name,service_logo as url from service where service_id=$target";
        }
        return daoCommon.selectCommon(sql, data);
    }
};
module.exports = dao;